package com.woniu.service.impl;

import com.woniu.entity.*;
import com.woniu.mapper.FoodMapper;
import com.woniu.mapper.FoodOrderMapper;
import com.woniu.mapper.OrderMapper;
import com.woniu.product.FoodProduct;
import com.woniu.service.FoodService;
import com.woniu.util.JWTUtil;
import com.woniu.util.ResData;
import com.woniu.util.ResEnum;
import com.woniu.util.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private FoodOrderMapper orderMapper;
    @Autowired
    private FoodProduct foodProduct;
    @Override
    public ResData<?> queryAll() {
        try {
            List<Food> foods = foodMapper.queryAll();
            return new ResData<>().ok(foods);
        } catch (Exception e) {
            return new ResData<>().fail(ResEnum.FAIL);
        }
    }

    @Override
    public ResData<?> addOrder(List<Food> foodList,String jwt) {
        try {
            FoodOrder order = new FoodOrder();
            //生成雪花Id
            SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator();
            long orderNum = snowflakeIdGenerator.nextId();
            order.setOrderNum(String.valueOf(orderNum));
            //获取userId
            Map userInfo = JWTUtil.getJWTUserInfo(jwt);
            Long userId = (Long) userInfo.get("userId");
            order.setUserId(userId.intValue());
            //计算总价
            BigDecimal totalPrice = BigDecimal.ZERO;
            for(Food f:foodList){
                Food food = foodMapper.queryFoodDetailById(f.getId());
                totalPrice = totalPrice.add(food.getPrice().multiply(new BigDecimal(f.getBuyNum())));
            }
            order.setTotalPrice(totalPrice);
            //插入订单
            orderMapper.addOrder(order);
            //生成订单详细表数据
            for(Food f:foodList){
                FoodOrderItem orderItem = new FoodOrderItem();
                Food food = foodMapper.queryFoodDetailById(f.getId());
                orderItem.setOrderNum(String.valueOf(orderNum));
                orderItem.setFoodId(f.getId());
                orderItem.setFoodName(food.getName());
                orderItem.setBuyNum(f.getBuyNum());
                orderItem.setPrice(food.getPrice());
                orderMapper.addOrderItem(orderItem);
            }
            //发送一个10秒延迟的取消订单操作
            foodProduct.send(String.valueOf(orderNum),"food",8000);
            return new ResData<>().ok(String.valueOf(orderNum));
        } catch (Exception e) {
            return new ResData<>().fail(ResEnum.FAIL);
        }
    }

    @Override
    public ResData<?> payOrder(String orderNum) {
        try {
            FoodOrder foodOrder = orderMapper.queryOrderByOrderNum(orderNum);
            if("0".equals(foodOrder.getStatus())){
                return new ResData<>().fail(ResEnum.FOOD_HAS_CANCELED);
            }else if("1".equals(foodOrder.getStatus())){
                orderMapper.updateOrder(orderNum,2);
            }else if("2".equals(foodOrder.getStatus())){
                return new ResData<>().fail(ResEnum.FOOD_HAS_PAYED);
            }
            return new ResData<>().ok();
        } catch (Exception e) {
            return new ResData<>().fail(ResEnum.FAIL);
        }
    }

}
